package api

import (
	"github.com/gin-gonic/gin"
	"hcy-api/lib/http"
	"hcy-api/structs/enum"
	"hcy-api/structs/vap"
	"vap/module/service"
)

var SessionApi = new(sessionApi)

type sessionApi struct {
}

func (a *sessionApi) SendInstruct(c *gin.Context) {
	in := struct {
		Id      int64  `json:"id,string"`
		Msg     string `json:"msg"`
		Title   string `json:"title"`
		MsgType string `json:"msgType"`
	}{}
	if err := c.ShouldBindJSON(&in); err != nil {
		http.BadWithParamsError(c, err)
		return
	}
	err := service.SessionService.SendInstruct(in.Id, in.Title, in.Msg, in.MsgType)
	if err != nil {
		http.BadWithCode(c, enum.ErrorMap.SendMsgErr)
		return
	}
	http.OK204(c)
}
func (a *sessionApi) Break(c *gin.Context) {
	input := struct {
		Id int64 `uri:"id"`
	}{}
	if err := c.ShouldBindUri(&input); err != nil {
		http.BadWithParamsError(c, err)
		return
	}
	if err := service.SessionService.Break(input.Id); err != nil {
		http.BadWithCode(c, enum.ErrorMap.GuacdErr)
		return
	}
	http.OK204(c)
}

func (a *sessionApi) Logoff(c *gin.Context) {
	input := struct {
		Id int64 `uri:"id"`
	}{}
	if err := c.ShouldBindUri(&input); err != nil {
		http.BadWithParamsError(c, err)
		return
	}
	if err := service.SessionService.Logoff(input.Id); err != nil {
		http.BadWithCode(c, enum.ErrorMap.GuacdErr)
		return
	}
	http.OK204(c)
}

func (a *sessionApi) GetSessionPageForAdmin(c *gin.Context) {
	var input vap.AdminSearchHostSession
	if err := c.ShouldBindQuery(&input); err != nil {
		http.BadWithParamsError(c, err)
		return
	}
	li, total, err := service.SessionService.GetSessionPageForAdmin(input)
	if err != nil {
		http.BadWithDB(c, err)
		return
	}
	http.OkPage(c, li, total)
}

func (a *sessionApi) InsertAppOpenLog(c *gin.Context) {
	input := struct {
		AppId         int64 `json:"appId,string"`
		HostSessionId int64 `json:"hostSessionId,string"`
	}{}
	if err := c.ShouldBindJSON(&input); err != nil {
		http.BadWithParamsError(c, err)
		return
	}
	if err := service.SessionService.InsertAppOpenLog(input.HostSessionId, input.AppId); err != nil {
		http.BadWithDB(c, err)
		return
	}
	http.OK204(c)
}

func (a *sessionApi) BindWinSessionId(c *gin.Context) {
	input := struct {
		HostSessionId int64 `json:"hostSessionId,string"`
		WinSessionId  int   `json:"winSessionId,string"`
	}{}
	if err := c.ShouldBindJSON(&input); err != nil {
		http.BadWithParamsError(c, err)
		return
	}
	if err := service.SessionService.BindWinSessionId(input.HostSessionId, input.WinSessionId); err != nil {
		http.BadWithDB(c, err)
		return
	}
	http.OK204(c)
}

func (a *sessionApi) CreateSession(c *gin.Context) {
	in := struct {
		AppId int64 `uri:"appId"`
	}{}
	if err := c.ShouldBindUri(&in); err != nil {
		http.BadWithParamsError(c, err)
		return
	}
	u := http.GetCurrentUser(c)
	rs, err := service.SessionService.CreateSession(u.UserID, in.AppId, u.Nickname)
	if err != nil {
		http.BadWithDB(c, err)
		return
	}
	http.Ok(c, rs)
}

func (a *sessionApi) CheckSessionReuse(c *gin.Context) {
	in := struct {
		AppId int64 `uri:"appId"`
	}{}
	if err := c.ShouldBindUri(&in); err != nil {
		http.BadWithParamsError(c, err)
		return
	}
	u := http.GetCurrentUser(c)
	rs, err := service.SessionService.CheckSessionReuse(u.UserID, in.AppId, u.Nickname)
	if err != nil {
		http.BadWithDB(c, err)
		return
	}
	http.Ok(c, rs)
}
func (a *sessionApi) Resize(c *gin.Context) {
	params := struct {
		Width     string `json:"width" form:"width"`
		Height    string `json:"height" form:"height"`
		SessionId int64  `json:"session_id,string" form:"session_id,string"`
	}{}
	if err := c.ShouldBindQuery(params); err != nil {
		http.BadWithParamsError(c, err)
		return
	}
	err := service.SessionService.Resize(params.Width, params.Height, params.SessionId)
	if err != nil {
		http.BadWithDB(c, err)
		return
	}
	http.OK204(c)
}
